#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include <mmsystem.h> 
#pragma comment(lib,"Winmm.lib")

IMAGE img_bk, img_bd1, img_bd2, img_bar_up1, img_bar_up2, img_bar_down1, img_bar_down2;  //定义背景，鸟，柱子
int jieshu=0;
int bird_x;
int bird_y;
int bar_x;
int bar_y;
  //得到变量

void startup()
{
	mciSendString("open D:\\background.mp3 alias bkmusic", NULL, 0, NULL);  //背景音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);  //循环播放

	initgraph(350, 600);
	loadimage(&img_bk, _T("D:\\background.jpg"));  //350*600
	loadimage(&img_bd2, _T("D:\\bird2.jpg"));		//34*24
	loadimage(&img_bd1, _T("D:\\bird1.jpg"));		
	loadimage(&img_bar_up1, _T("D:\\bar_up1.gif"));  //140*600
	loadimage(&img_bar_up2, _T("D:\\bar_up2.gif"));
	loadimage(&img_bar_down1, _T("D:\\bar_down1.gif"));  //140*600
	loadimage(&img_bar_down2, _T("D:\\bar_down2.gif"));  //导入图像
	bird_x = 50;
	bird_y = 200;
	bar_x = 300;
	bar_y = (rand() % 350)-600;
	BeginBatchDraw();  //开画
	
	
}  //数据初始化


void show()
{
	if(jieshu==0)
	{
		putimage(0, 0, &img_bk);  //背景
		putimage(bar_x,bar_y, &img_bar_up1,NOTSRCERASE);  //上一半障碍物
		putimage(bar_x, bar_y, &img_bar_up2,SRCINVERT);
		putimage(bar_x, bar_y+700, &img_bar_down1,NOTSRCERASE);  //下一半障碍物
		putimage(bar_x, bar_y+700, &img_bar_down2, SRCINVERT);
		putimage(bird_x, bird_y, &img_bd1, NOTSRCERASE); //小鸟
		putimage(bird_x,bird_y, &img_bd2, SRCINVERT);
		FlushBatchDraw();  //批量制图
		Sleep(50);
	}
	else
	{
		EndBatchDraw();
		putimage(0, 0, &img_bk);  //背景
		putimage(160, 0, &img_bd1, NOTSRCERASE); //小鸟O
		putimage(160,0, &img_bd2, SRCINVERT);
		putimage(125, 25, &img_bd1, NOTSRCERASE); 
		putimage(125,25, &img_bd2, SRCINVERT);
		putimage(195, 25, &img_bd1, NOTSRCERASE); 
		putimage(195,25, &img_bd2, SRCINVERT);
		putimage(125, 50, &img_bd1, NOTSRCERASE); 
		putimage(125,50, &img_bd2, SRCINVERT);
		putimage(195, 50, &img_bd1, NOTSRCERASE); 
		putimage(195,50, &img_bd2, SRCINVERT);
		putimage(160, 75, &img_bd1, NOTSRCERASE); 
		putimage(160,75, &img_bd2, SRCINVERT);

		putimage(125, 150, &img_bd1, NOTSRCERASE);   //小鸟V
		putimage(125,150, &img_bd2, SRCINVERT);
		putimage(195, 150, &img_bd1, NOTSRCERASE); 
		putimage(195,150, &img_bd2, SRCINVERT);
		putimage(140, 185, &img_bd1, NOTSRCERASE); 
		putimage(140,185, &img_bd2, SRCINVERT);
		putimage(184, 185, &img_bd1, NOTSRCERASE); 
		putimage(184,185, &img_bd2, SRCINVERT);
		putimage(160, 225, &img_bd1, NOTSRCERASE); 
		putimage(160,225, &img_bd2, SRCINVERT);

		putimage(160, 300, &img_bd1, NOTSRCERASE);   //小鸟E
		putimage(160,300, &img_bd2, SRCINVERT);
		putimage(190, 322, &img_bd1, NOTSRCERASE); 
		putimage(190,322, &img_bd2, SRCINVERT);
		putimage(134, 322, &img_bd1, NOTSRCERASE); 
		putimage(134,322, &img_bd2, SRCINVERT);
		putimage(195, 350, &img_bd1, NOTSRCERASE); 
		putimage(195,350, &img_bd2, SRCINVERT);
		putimage(125, 350, &img_bd1, NOTSRCERASE); 
		putimage(125,350, &img_bd2, SRCINVERT);
		putimage(160, 350, &img_bd1, NOTSRCERASE); 
		putimage(160,350, &img_bd2, SRCINVERT);
		putimage(134, 378, &img_bd1, NOTSRCERASE); 
		putimage(134,378, &img_bd2, SRCINVERT);
		putimage(160, 400, &img_bd1, NOTSRCERASE);  
		putimage(160,400, &img_bd2, SRCINVERT);

		putimage(134, 450, &img_bd1, NOTSRCERASE);   //小鸟R
		putimage(134,450, &img_bd2, SRCINVERT);
		putimage(134, 475, &img_bd1, NOTSRCERASE);   
		putimage(134,475, &img_bd2, SRCINVERT);
		putimage(134, 500, &img_bd1, NOTSRCERASE);   
		putimage(134,500, &img_bd2, SRCINVERT);
		putimage(134, 525, &img_bd1, NOTSRCERASE);   
		putimage(134,525, &img_bd2, SRCINVERT);
		putimage(190, 450, &img_bd1, NOTSRCERASE);   
		putimage(190,450, &img_bd2, SRCINVERT);
		putimage(175, 475, &img_bd1, NOTSRCERASE);   
		putimage(175,475, &img_bd2, SRCINVERT);
		putimage(160, 500, &img_bd1, NOTSRCERASE);   
		putimage(160,500, &img_bd2, SRCINVERT);
		system("pause");  //任意键结束
		exit(0);   //退出游戏
	}
	
}  //显示函数

void upwhitoutInput()
{
	
	if (bird_y < 580)
	{
		bird_y = bird_y + 3;
	}
	if (bar_x >-140)
	{
		bar_x -= 5;
	}
	if (bar_x<= -140)
	{
		bar_x = 350;
		bar_y = (rand() % 350) - 600;
	}
	if (((bird_x >= (bar_x-20)&&bird_x <= (bar_x +140)) && ((bird_y <= bar_y+600) || (bird_y >= bar_y+676)) || bird_y >=580))
	{
		jieshu++;
	}
	//if ((bird_x = (bar_x + 150))&& ((bird_y > bar_y + 600) && (bird_y < bar_y + 676)))
	//{
	//	score++;
	//}
}  //无键入更新

void upwhitInput()
{
	char input;
	if (_kbhit())  //当按键开始
	{

		input = _getch();
		if (input == 'w' && bird_y > 20)
		{
			bird_y -= 30;   //小鸟上升
			
			mciSendString("close jpmusic", NULL, 0, NULL);  //先关闭前一次
			mciSendString("open D:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);  //跳跃音乐
			mciSendString("play jpmusic", NULL, 0, NULL);  //仅播放一次
		}
		if (input == 's' && bird_y <580)
		{
			bird_y += 30;   //小鸟下降

			mciSendString("close jpmusic", NULL, 0, NULL);  //先关闭前一次
			mciSendString("open D:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);  //跳跃音乐
			mciSendString("play jpmusic", NULL, 0, NULL);  //仅播放一次
		}

	}

}  //有键入更新

void gameover()
{


}

int main()
{
		startup();  //数据初始化
		while (1)
		{
			show();  //显示
			upwhitoutInput();  //更新无输入
			upwhitInput();  //更新有输入
		}
		gameover();  //游戏结束与后续操作
	
		return 0;
}